import sqliteFriend from '@/sqlite_services/friend.js'
import $store from '@/store/index.js';
import friendsBlackSqlite from '@/sqlite_services/friendsBlack.js'
import groupMembersSqlite from '@/sqlite_services/group_members.js'
export default {
	/* 好友列表 */
	async friendList(requestError = true) {
		try{
			let res = await uni.$api.friends.list(requestError)
			if(res){
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	//获取在线好友
	async onlineFriend(){
		try{
			let res = await uni.$api.friends.onlineFriend()
			if(res){
				return res.data.list
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 获取好友信息 */
	async getFriendInfo(frienId) {
		try{
			let res = await uni.$api.friends.friendInfo({friend_id:frienId})
			if(res){
				if(res.data.friend_group_info){
					res.data.friend_group_id = res.data.friend_group_info.group_id
					res.data.friend_group_name = res.data.friend_group_info.group_name
				}else{
					res.data.friend_group_id = 0
					res.data.friend_group_name = ''
				}
				delete res.data.friend_group_info
				let sqldata = JSON.parse(JSON.stringify(res.data))
				delete sqldata.is_black
				sqliteFriend.addOrEditUserInfo(sqldata)
				//如果是拉黑状态如果黑名单没有就添加，有就不管了
				if(res.data.is_black){
					friendsBlackSqlite.addBlackInfo({
						black_id:frienId
					})
				}else{//如果黑名单有就删除没有就不管了
					friendsBlackSqlite.delBlackInfo(frienId)
				}
				//更新正在聊天中的信息
				$store.commit('updateChatInfo', { uuid:frienId, data: {
					nickname: res.data.friend_remark || res.data.nick_name,
					avatar_url: res.data.avatar_url,
				}});
				
				
				$store.dispatch('updateSession',{
					chat_id:frienId, 
					data:{
						chat_name:res.data.friend_remark || res.data.nick_name,
						nick_name:res.data.nick_name,
						chat_face:res.data.avatar_url,
						chat_member_type:res.data.user_type
					}
				})
				//更新好友列表
				$store.dispatch('updateFriendList', { friend_idx:frienId, data: {
					nick_name: res.data.nick_name,
					avatar_url: res.data.avatar_url,
					friend_remark:res.data.friend_remark,
					friend_group: res.data.friend_group_id || 0
				}});
				
				//更新群成员
				this.uploadGroupMember(frienId,{
					user_id:frienId,
					user_nickname:res.data.nick_name,
					avatar_url:res.data.avatar_url,
					user_type:res.data.user_type,
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 通过用户号查找信息 */
	async userNoFriendInfo(user_no) {
		try{
			let res = await uni.$api.friends.findUser({find_user_no:user_no})
			if(res){
				if (Object.keys(res.data).length) {
				    let sqlupdata = JSON.parse(JSON.stringify(res.data))
				    delete sqlupdata.is_friend
				    sqliteFriend.addOrEditUserInfo(sqlupdata)
					//更新群成员
					this.uploadGroupMember(res.data.user_id,{
						user_id:res.data.user_id,
						user_nickname:res.data.nick_name,
						avatar_url:res.data.avatar_url,
						user_type:res.data.user_type,
					})
				    return [res.data]
				}
				return []
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	
	/* 获取用户信息任何可以不是好友 */
	async getUserInfo(target_id) {
		try{
			let res = await uni.$api.user.userInfo({target_id:target_id})
			if(res){
				sqliteFriend.addOrEditUserInfo(res.data)
				//更新群成员
				this.uploadGroupMember(target_id,{
					user_id:target_id,
					user_nickname:res.data.nick_name,
					avatar_url:res.data.avatar_url,
					user_type:res.data.user_type,
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 好友审核 */
	async auditing(id,requester_user_id,req_status) {
		try{
			let res = await uni.$api.friends.auditing({id,requester_user_id,req_status})
			if(res){
				console.log(res);
				uni.showToast({
					title:res.msg,
					icon:'none'
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	//修改备注
	async editNickname({friend_id,friend_nickname,nickname}) {
		try{
			let res = await uni.$api.friends.editNickname({friend_id,friend_nickname})
			if(res){
				//更新好友信息
				sqliteFriend.editUserInfo(friend_id,{
					friend_remark:friend_nickname
				})
				//更新正在聊天中的信息
				$store.commit('updateChatInfo', { uuid:friend_id, data: {
					nickname: friend_nickname || nickname
				}});
				//更新会话
				$store.dispatch('updateSession',{
					chat_id:friend_id, 
					data:{
						chat_name:friend_nickname || nickname
					}
				})
				//更新好友列表
				$store.dispatch('updateFriendList', { friend_idx:friend_id, data: {
					friend_remark:friend_nickname
				}});
				uni.showToast({
					title:res.msg,
					icon:'none'
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	//更新群成员信息
	uploadGroupMember(user_id,data){
		//发送全局通知群相关页面修改资料
		uni.$emit('onGroupMemberAction',{
			type:'userInfo',
			data
		})
		$store.commit('updateGroupMembersInfo',{
			group_id:null, 
			data,
			noVerify:true
		})
		groupMembersSqlite.editGroupMembersInfo(user_id,data)
	}
}